package com.pc.ipushpin.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtils {
	
	
	public static boolean compareDay(Date startTime, Date endTime, int day) {
		if (startTime == null || endTime == null) return false;
		
		if ((endTime.getTime() - startTime.getTime()) > (day * 86400000)) return true;
		else return false;
	}
	
	public static String getDateTimeString() {
		
		return getDateString(new Date(), "yyyyMMddHHmmss");
	}

	public static String getDateString(Date date, String pattern) {

		SimpleDateFormat df = null;
		String returnValue = "";

		if (date != null) {
			df = new SimpleDateFormat(pattern);
			returnValue = df.format(date);
		}

		return returnValue;
	}
	
	public static Date getDate(String dateStr) {
		if (null == dateStr || "".equals(dateStr)) {
			return null;
		}
		
		dateStr = dateStr.trim();
		
		String[] dateTimeReg = {
					 "\\s*\\d{1,4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2}\\s*", 
					 "\\s*\\d{1,4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}:\\d{1,2}\\s*",
					 "\\s*\\d{1,4}-\\d{1,2}-\\d{1,2}\\s+\\d{1,2}\\s*",
					 "\\s*\\d{1,4}-\\d{1,2}-\\d{1,2}\\s*",
					 "\\s*\\d{1,4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}:\\d{1,2}:\\d{1,2}\\s*", 
					 "\\s*\\d{1,4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}:\\d{1,2}\\s*",
					 "\\s*\\d{1,4}/\\d{1,2}/\\d{1,2}\\s+\\d{1,2}\\s*",
					 "\\s*\\d{1,4}/\\d{1,2}/\\d{1,2}\\s*",
					 "\\s*\\d{1,14}\\s*"};
		
		
		DateFormat[] dateFormat = {
					new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
					new SimpleDateFormat("yyyy-MM-dd HH:mm"),
					new SimpleDateFormat("yyyy-MM-dd HH"),
					new SimpleDateFormat("yyyy-MM-dd"),
					new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"),
					new SimpleDateFormat("yyyy/MM/dd HH:mm"),
					new SimpleDateFormat("yyyy/MM/dd HH"),
					new SimpleDateFormat("yyyy/MM/dd"),
					new SimpleDateFormat("yyyyMMddHHmmss")
		};
		
		Pattern pattern = null;
		int i=0;
		for (; i<dateTimeReg.length; i++) {
			
			pattern = Pattern.compile(dateTimeReg[i]);
			Matcher mat = pattern.matcher(dateStr);  
			boolean isMatch = mat.find();
			if (isMatch)
				break;
		}
		
		Date date = null;
		
		if (i < dateTimeReg.length) {
			try {
				date = dateFormat[i].parse(dateStr);
			} catch (ParseException e) {
				date = null;
			}
		}
		
		return date;
	}
	
	/**
	   * 将短时间格式字符串转换为时间 yyyy-MM-dd 
	   * 
	   * @param strDate
	   * @return
	   */
	public static Date strToDate(String strDate) {
	   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
	   ParsePosition pos = new ParsePosition(0);
	   Date strtodate = formatter.parse(strDate, pos);
	   return strtodate;
	}
	  /**
     * 取得当前时间戳（精确到秒）
     *
     * @return nowTimeStamp
     */
    public static String getNowTimeStamp() {
        long time = System.currentTimeMillis();
        String nowTimeStamp = String.valueOf(time / 1000);
        return nowTimeStamp;
    }
    
	/**
	 * 获取过期时间
	 * @param overSecond
	 * @return
	 */
	public static String getEndTime(Date date,Integer overSecond) {
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.err.println(sdf.format(date));
		Calendar calendar = Calendar.getInstance();    
	    calendar.setTime(date);    
	    calendar.add(Calendar.SECOND, overSecond);
	    Date endTime =  calendar.getTime();
	    System.err.println(sdf.format(endTime));
	    return sdf.format(endTime);
	}
	
	/**
	 * 判断是否是当天日期
	 * @param date
	 * @return
	 */
	public static boolean isToday(Date date){
		
		boolean result = false;
		Calendar nowCalendar = Calendar.getInstance();              
		nowCalendar.setTime(new Date());                                 
		int nowYear = nowCalendar.get(Calendar.YEAR);
		int nowMonth = nowCalendar.get(Calendar.MONTH)+1;
		int nowDay = nowCalendar.get(Calendar.DAY_OF_MONTH);     
		
		Calendar calendar = Calendar.getInstance();              
		calendar.setTime(date);                                 
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH)+1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		if(year==nowYear && month==nowMonth && day==nowDay){
			result = true;
		}
		
		return result;
	}
	
	public static String secondToString(Integer s) {
		if(s != null) {
			if(s > 0) {
				StringBuffer sb = new StringBuffer("");
				
		        int hour = 0;
		        int minute = 0;
		        int second = 0;
		         
		        second = s % 60;
		        hour = s / 3600;
		        minute = s / 60;
		        
		        if(hour != 0) {
		        	sb.append(hour + ":");
		        }
		        if(minute != 0) {
		        	sb.append(minute + ":");
		        } 
		        if(second != 0) {
		        	sb.append(second);
		        }
		        
		        return sb.toString();
			} else {
				return "0";
			}
		} else {
			return "0";
		}
	}

	public static String getDateLocation(Date currentDate) {
//		long yd = 1000 * 24 * 60 * 60 * 30 * 12; //每年毫秒数

//		long md = 1000 * 24 * 60 * 60 * 30; //每月毫秒数

		long nd = 1000 * 24 * 60 * 60;//每天毫秒数

		long nh = 1000 * 60 * 60;//每小时毫秒数

		long nm = 1000 * 60;//每分钟毫秒数

		long diff =  new Date().getTime() - currentDate.getTime(); // 获得两个时间的毫秒时间差异

//		long year = diff / yd;

//		long month = diff / md;
		long year = diff / (nd * 30 * 12); //计算差多少年

		long month = diff / (nd * 30); //计算差多少月

		long day = diff / nd;   // 计算差多少天

		long hour = diff % nd / nh; // 计算差多少小时
		long min = diff % nd % nh / nm;  // 计算差多少分钟

		if(year < 1 ) {
			if(month < 1 ) {
				if(day <= 1) {
					if(hour < 1) {
						if(min < 1) {
							return "刚刚";
						} else {
							return min + "分钟前";
						}
					} else {
						return hour + "小时前";
					}
				} else {
					return day + "天前";
				}
			}	else {
				return month + "月前";
			}
		}  else  {
			return year + "年前";
		}
	}
}
